package com.gm.commonlib.base.baseui.baseFragment;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.DrawableRes;
import androidx.annotation.Nullable;

import com.gm.commonlib.R;
import com.gm.commonlib.util.logUtils.LogUtil;
import com.gm.commonlib.views.dialog.loadingDialog.ILoadingDialog;
import com.gm.commonlib.views.dialog.loadingDialog.LoadingDialogFactory;
import com.gm.commonlib.views.toast.IToast;
import com.gm.commonlib.views.toast.ToastFactory;

public class BaseFragment extends CommonAbstractBaseFragment{

    protected ViewGroup rootView;
    View content;
    private ViewGroup fl_container_title;
    private ViewGroup fl_container;
    protected ViewGroup fl_container_header;
    protected ViewGroup fl_list_container;
    protected ViewGroup fl_container_footer;
    public boolean mIsCreateViewFinish;

    public boolean isCreateViewFinish(){
        return mIsCreateViewFinish;
    }



    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        rootView = (ViewGroup) inflater.inflate(R.layout.fragment_base, null);
        content = rootView.findViewById(R.id.content);
        context=getContent();
        fl_container_header = rootView.findViewById(R.id.fl_container_header);
        fl_list_container = rootView.findViewById(R.id.fl_list_container);
        fl_container_footer = rootView.findViewById(R.id.fl_container_footer);
        fl_container_title = rootView.findViewById(R.id.fl_container_title);
        fl_container = rootView.findViewById(R.id.fl_container);

        onCreateContentTitle(fl_container_title); //title
        onCreateContentContainer(fl_container); //title下面，header上面
        onCreateContentHeader(fl_container_header);//title下面 container 下面部分

        onCreateContentBody(fl_list_container);//内容部分，在header下面

        onCreateContentFooter(fl_container_footer);//底部区域
        onPostCreateContentView(rootView, savedInstanceState);
        onPostCreateContentView(rootView, savedInstanceState);
        //把点击事件屏蔽了，避免下层的ui获取事件(不然多个fragment在一个act时候，会出现，点击上面fragment
        // 页面，底层的fragment获取事件)
        rootView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return true;
            }
        });
        onCreateViewFinish();
        return rootView;
    }
    protected void onPostCreateContentView(ViewGroup parent, Bundle savedInstanceState) {

    }

    public void onCreateViewFinish(){

    }


    protected void setBackgroundResource(@DrawableRes int resId) {
        if (content != null) {
            content.setBackgroundResource(resId);
        }
    }


    /**
     * 创建容器头部容器</br>
     *
     * @param parent 向parent设置滑动布局 内容
     */
    protected void onCreateContentHeader(ViewGroup parent) {
    }

    protected void onCreateContentTitle(ViewGroup parent) {
    }

    protected void onCreateContentContainer(ViewGroup parent) {
    }

    ;


    /**
     * 提供默认的创建刷新界面的处理</br>
     *
     * @param parent 向parent设置滑动布局 内容
     */
    protected void onCreateContentBody(ViewGroup parent) {
    }


    /**
     * 创建容器脚部容器</br>
     *
     * @param parent 向parent设置滑动布局 内容
     */
    protected void onCreateContentFooter(ViewGroup parent) {
    }
    @Override
    ILoadingDialog createLoadingDialog() {
        return LoadingDialogFactory.newInstance();
    }

    @Override
    IToast createToast() {
        return ToastFactory.newInstance();
    }

    @Override
    public Context getContent() {
        return getActivity();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        LogUtil.d("销毁: "+getFragmentName());
    }

    public String getFragmentName(){
        return getClass().getName();
    }
}
